package com.suse.lhw.hms.authserver.handler;


import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ProjectName: health_manage_system
 * @Package: com.suse.lhw.hms.authserver.config
 * @ClassName: FailureAurhorizationHandler
 * @Description:作为登录失败的处理器，提供对WebSercurity失败登录的支持
 * @Author: 李鸿炜
 * @CreateDate: 2020/4/2 9:16
 * @UpdateUser: 更新者
 * @UpdateDate: 2020/4/2 9:16
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */

@Component
public class FailureAurhorizationHandler implements AuthenticationFailureHandler {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    //    @Autowired
//    private ObjectMapper objectMapper;
    @Override
    public void onAuthenticationFailure(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {
        logger.info("登录失败");
        httpServletResponse.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        httpServletResponse.getWriter().write(e.toString());
//        httpServletResponse.getWriter().write(objectMapper.writeValueAsString(e));
        //    httpServletResponse.getWriter().write(objectMapper.writeValueAsString(new SimpleResonse(e.getMessage())));
    }
}
